<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Record;
use Excel;
use Illuminate\Support\Facades\Schema;

class DetailRecordController extends Controller
{
    public function index(Request $request)
    {

        $where = function ($query) use ($request) {
            if ($request->has('mobile') and $request->mobile != '') {
                $query->where('mobile', $request->mobile);
            }

            if ($request->has('status') and $request->status != '-100') {
                if ($request->status == -1) {
                    $query->whereIn('status', [-2, -1]);
                } else
                    $query->where('status', $request->status);
            }

            if ($request->has('name') and $request->name != '') {
                $query->where('name', $request->name);
            }

            if($request->has('created_at') and $request->created_at !=''){
                $time = explode('~',$request->created_at);
                $time[] = $time[0].'00:00:00';
                $time[] = $time[1].'23:59:59';
                $query->whereBetween('create_time',$time);
            }
        };

        $records = Record::where('origin', 'DETAIL')
            ->where('status', '<>', '-3')->where($where)->orderBy('create_time', 'desc')
            ->paginate(10);

        $status = config('system.status');
        $status1 = config('system.status1');
        return view('admin.detailrecord.index', compact('records', 'status', 'status1'));
    }

    public function status(Request $request)
    {
        $record = Record::find($request->id);
        $record->increment('status');

        if ($request->has('is_sendmessage') and ($request->is_sendmessage == 1)) {

        }

        if ($record->save()) {
            return redirect()->to(route('fastApply.index'))
                ->with('notice',"改变状态成功！");
        } else {
            return redirect()->back()
                ->with('notice',"改变状态失败！");
        }

    }

    public function destroy(Request $request)
    {
        $record = Record::find($request->id);
        if ($record->delete()) {
            return redirect()->to(route('fastApply.index'))->with('notice', '删除成功！');
        } else {
            return back()->with('notice', '删除失败！');
        }
    }

    //软删除改变status值为-3
    public function delete(Request $request)
    {
        $record = Record::find($request->id);
        $record->status = -3;
        if ($record->save()) {
            return redirect(route('fastApply.index'))->with('notice', '软删除成功！');
        } else {
            return back()->with('notice', '软删除失败！');
        }
    }

    public function exportExcel(Request $request){
        $where = function ($query) use ($request) {
            if ($request->has('mobile') and $request->mobile != '') {
                $query->where('mobile', $request->mobile);
            }

            if ($request->has('status') and $request->status != '-100') {
                if ($request->status == -1) {
                    $query->whereIn('status', [-2, -1]);
                } else
                    $query->where('status', $request->status);
            }

            if ($request->has('name') and $request->name != '') {
                $query->where('name', $request->name);
            }

            if($request->has('created_at') and $request->created_at !=''){
                $time = explode('~',$request->created_at);
                $time[] = $time[0].'00:00:00';
                $time[] = $time[1].'23:59:59';
                $query->whereBetween('create_time',$time);
            }
        };

        $columns = Schema::getColumnListing('records');
        //dd($columns);

        $records = Record::where('origin', 'DETAIL')
            ->where('status', '<>', '-3')->where($where)->orderBy('create_time', 'desc')
            ->get();
        $records = $records->toArray();
        $filename = date("Y-m-d H:i:s");
        //return $records;
        Excel::create($filename, function ($excel) use($records,$columns) {
            array_unshift($records,$columns);
            $cellData = $records;
            $excel->sheet('score', function ($sheet) use ($cellData) {
                $sheet->rows($cellData);
            });
        })->export('xls');
    }
}


